package com.zc.iotDevice.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.zc.iotDevice.domain.IotDeviceSubsidiary;
import com.zc.iotDevice.domain.IotDeviceSubsidiaryPort;
import com.zc.iotDevice.mapper.IotDeviceSubsidiaryMapper;
import com.zc.iotDevice.service.IIotDeviceSubsidiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备附属Service业务层处理
 *
 * @author gaojikun
 * @date 2023-05-16
 */
@Service
public class IotDeviceSubsidiaryServiceImpl implements IIotDeviceSubsidiaryService {
    @Autowired
    private IotDeviceSubsidiaryMapper iotDeviceSubsidiaryMapper;

    /**
     * 查询设备附属
     *
     * @param id 设备附属主键
     * @return 设备附属
     */
    @Override
    public IotDeviceSubsidiary selectIotDeviceSubsidiaryById(Long id) {
        return iotDeviceSubsidiaryMapper.selectIotDeviceSubsidiaryById(id);
    }

    /**
     * 查询设备附属列表
     *
     * @param iotDeviceSubsidiary 设备附属
     * @return 设备附属
     */
    @Override
    public List<IotDeviceSubsidiary> selectIotDeviceSubsidiaryList(IotDeviceSubsidiary iotDeviceSubsidiary) {
        List<IotDeviceSubsidiary> list = iotDeviceSubsidiaryMapper.selectIotDeviceSubsidiaryList(iotDeviceSubsidiary);
        for (IotDeviceSubsidiary i : list) {
            if (!StringUtils.isEmpty(i.getDeviceType())) {
                String device_type = DictUtils.getDictLabel("device_type", i.getDeviceType());
                i.setDeviceTypeName(device_type);
            }
            if (!StringUtils.isEmpty(i.getCurrentType())) {
                String device_type = DictUtils.getDictLabel("current_type", i.getCurrentType());
                i.setCurrentTypeName(device_type);
            }
        }
        return list;
    }

    /**
     * 新增设备附属
     *
     * @param iotDeviceSubsidiary 设备附属
     * @return 结果
     */
    @Override
    public int insertIotDeviceSubsidiary(IotDeviceSubsidiary iotDeviceSubsidiary) {
        iotDeviceSubsidiary.setCreateTime(DateUtils.getNowDate());
        iotDeviceSubsidiary.setCreateName(SecurityUtils.getUsername());
        return iotDeviceSubsidiaryMapper.insertIotDeviceSubsidiary(iotDeviceSubsidiary);
    }

    /**
     * 修改设备附属
     *
     * @param iotDeviceSubsidiary 设备附属
     * @return 结果
     */
    @Override
    public int updateIotDeviceSubsidiary(IotDeviceSubsidiary iotDeviceSubsidiary) {
        iotDeviceSubsidiary.setUpdateTime(DateUtils.getNowDate());
        iotDeviceSubsidiary.setUpdateName(SecurityUtils.getUsername());
        return iotDeviceSubsidiaryMapper.updateIotDeviceSubsidiary(iotDeviceSubsidiary);
    }

    /**
     * 批量删除设备附属
     *
     * @param ids 需要删除的设备附属主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceSubsidiaryByIds(Long[] ids) {
        return iotDeviceSubsidiaryMapper.deleteIotDeviceSubsidiaryByIds(ids);
    }

    /**
     * 删除设备附属信息
     *
     * @param id 设备附属主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceSubsidiaryById(Long id) {
        return iotDeviceSubsidiaryMapper.deleteIotDeviceSubsidiaryById(id);
    }

    /**
     * 查询运营商列表
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult getOperatorList() {
        List<Map<String, Object>> list = iotDeviceSubsidiaryMapper.getOperatorList(new BaseEntity());
        return AjaxResult.success(list);
    }

    /**
     * 查询设备列表
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult getIotDeviceList() {
        List<Map<String, Object>> list = iotDeviceSubsidiaryMapper.getIotDeviceList();
        return AjaxResult.success(list);
    }

    /**
     * 设备类型
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult getDeviceTypeList() {
        List<SysDictData> device_type = DictUtils.getDictCache("device_type");
        List<Map<String, String>> list = new ArrayList<>();
        for (SysDictData s : device_type) {
            list.add(new HashMap<String, String>() {{
                put("id", s.getDictValue());
                put("name", s.getDictLabel());
            }});
        }
        return AjaxResult.success(list);
    }

    /**
     * 直流交流类型
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult getCurrentTypeList() {
        List<SysDictData> device_type = DictUtils.getDictCache("current_type");
        List<Map<String, String>> list = new ArrayList<>();
        for (SysDictData s : device_type) {
            list.add(new HashMap<String, String>() {{
                put("id", s.getDictValue());
                put("name", s.getDictLabel());
            }});
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取设备附属端口信息
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectIotDeviceSubsidiaryPortListById(Long id) {
        List<IotDeviceSubsidiaryPort> portList = iotDeviceSubsidiaryMapper.selectIotDeviceSubsidiaryPortListById(id);
        return AjaxResult.success(portList);
    }

    /**
     * 修改设备附属端口
     */
    @Override
    public AjaxResult updateSubsidiaryPort(IotDeviceSubsidiaryPort iotDeviceSubsidiaryPort) {
        //端口名称查重
        List<IotDeviceSubsidiaryPort> list = iotDeviceSubsidiaryMapper.selectIotDeviceSubsidiaryPortListById(iotDeviceSubsidiaryPort.getDeviceId());
        if (list != null && list.size() > 0) {
            for (IotDeviceSubsidiaryPort i : list) {
                if (!iotDeviceSubsidiaryPort.getId().equals(i.getId())
                        && iotDeviceSubsidiaryPort.getPortName().equals(i.getPortName())) {
                    return AjaxResult.error("端口名称重复");
                }
            }
        }


        iotDeviceSubsidiaryPort.setUpdateName(SecurityUtils.getUsername());
        iotDeviceSubsidiaryPort.setUpdateTime(DateUtils.getNowDate());
        boolean isUpdate = iotDeviceSubsidiaryMapper.updateSubsidiaryPort(iotDeviceSubsidiaryPort);
        if (isUpdate) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 删除设备附属端口
     */
    @Override
    public AjaxResult delSubsidiaryPort(Long id) {
        if (id == null) {
            return AjaxResult.error("参数为空");
        }
        boolean isUpdate = iotDeviceSubsidiaryMapper.delSubsidiaryPort(id);
        if (isUpdate) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 新增设备附属端口
     */
    @Override
    public AjaxResult addSubsidiaryPort(IotDeviceSubsidiaryPort iotDeviceSubsidiaryPort) {
        //端口名称查重
        List<IotDeviceSubsidiaryPort> list = iotDeviceSubsidiaryMapper.selectIotDeviceSubsidiaryPortListById(iotDeviceSubsidiaryPort.getDeviceId());
        if (list != null && list.size() > 0) {
            for (IotDeviceSubsidiaryPort i : list) {
                if (iotDeviceSubsidiaryPort.getPortName().equals(i.getPortName())) {
                    return AjaxResult.error("端口名称重复");
                }
            }
        }


        iotDeviceSubsidiaryPort.setCreateName(SecurityUtils.getUsername());
        iotDeviceSubsidiaryPort.setCreateTime(DateUtils.getNowDate());
        boolean isUpdate = iotDeviceSubsidiaryMapper.addSubsidiaryPort(iotDeviceSubsidiaryPort);
        if (isUpdate) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 获取设备附属端口详细信息
     */
    @Override
    public AjaxResult getSubsidiaryPort(Long id) {
        IotDeviceSubsidiaryPort iotDeviceSubsidiaryPort = iotDeviceSubsidiaryMapper.getSubsidiaryPort(id);
        return AjaxResult.success(iotDeviceSubsidiaryPort);
    }


}
